package com.collect.senegal.zhiGlobal.zhiHandler;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

import com.kaopiz.kprogresshud.KProgressHUD;
import com.collect.senegal.zhiUtils.StringUtil;

public class ZHIProgressHUDHelper extends Handler {

    public static final int FT_SHOW_HUD = 1;
    public static final int FT_DISMISS_HUD = 2;

    //无用code....
    private int CFProgressHUDHelpera__;
    private int CFProgressHUDHelperb__;
    private int CFProgressHUDHelperc__;
    private int CFProgressHUDHelperd__;
    
    protected KProgressHUD mKProgressHUD;

    private Context context;
    private boolean cancelable;
    private String label;
    private OnFTProgressHUDCancelListener mOnFTProgressHUDCancelListener;

    public ZHIProgressHUDHelper(Context context, OnFTProgressHUDCancelListener onFTProgressHUDCancelListener, boolean cancelable) {
        super();
        this.context = context;
        this.mOnFTProgressHUDCancelListener = onFTProgressHUDCancelListener;
        this.cancelable = cancelable;

        CFProgressHUDHelper1();
        CFProgressHUDHelper2();
        CFProgressHUDHelper3();
        CFProgressHUDHelper4();
    }

    public ZHIProgressHUDHelper(Context context, String label) {
        super();
        this.context = context;
        this.mOnFTProgressHUDCancelListener = null;
        this.cancelable = false;
        this.label = label;

        CFProgressHUDHelper1();
        CFProgressHUDHelper2();
        CFProgressHUDHelper3();
        CFProgressHUDHelper4();
    }

    public void setHUDLabel(String label){
        this.label = label;
        if (mKProgressHUD != null) {
            mKProgressHUD.setLabel(label);
        }

        CFProgressHUDHelpera__++;
        CFProgressHUDHelperb__++;
        CFProgressHUDHelperc__--;

        if(CFProgressHUDHelpera__>10){
            return;
        }

        if(CFProgressHUDHelperb__>10){
            return;
        }

        if(CFProgressHUDHelperd__>10){
            return;
        }
    }
    
    private void initProgressDialog(){
        if (mKProgressHUD == null){
            mKProgressHUD = KProgressHUD.create(context).setStyle(KProgressHUD.Style.SPIN_INDETERMINATE);
            mKProgressHUD.setAnimationSpeed(2)
                    .setDimAmount(0.5f)
                    .setSize(100, 100);
        }

        if (!StringUtil.isEmpty(this.label)){
            mKProgressHUD.setLabel(this.label);
        }

        try {
            mKProgressHUD.show();
        }
        catch (Exception e){

        }

        CFProgressHUDHelperd__++;
        CFProgressHUDHelperc__++;

        if(CFProgressHUDHelperd__>10){
            CFProgressHUDHelpera__++;
            return;
        }

        if(CFProgressHUDHelperc__>10){
            CFProgressHUDHelperb__++;
            return;
        }
    }

    private void dismissHUD(){
        if (mKProgressHUD != null) {
            mKProgressHUD.dismiss();
            mKProgressHUD = null;
        }

        if(CFProgressHUDHelperb__>10){
            CFProgressHUDHelperc__++;
            return;
        }
    }

    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case FT_SHOW_HUD:
                initProgressDialog();
                break;
            case FT_DISMISS_HUD:
                dismissHUD();
                break;
        }
    }


    public interface OnFTProgressHUDCancelListener {
        void onCancelProgress();
    }

    public void show(){
        this.obtainMessage(FT_SHOW_HUD)
                .sendToTarget();
    }
    public void hide(){
        this.obtainMessage(FT_DISMISS_HUD)
                .sendToTarget();
    }

    public void destroy(){
        dismissHUD();
    }

    public int getCFProgressHUDHelpera__() {
        return CFProgressHUDHelpera__;
    }

    public void setCFProgressHUDHelpera__(int CFProgressHUDHelpera__) {
        this.CFProgressHUDHelpera__ = CFProgressHUDHelpera__;
    }

    public int getCFProgressHUDHelperb__() {
        return CFProgressHUDHelperb__;
    }

    public void setCFProgressHUDHelperb__(int CFProgressHUDHelperb__) {
        this.CFProgressHUDHelperb__ = CFProgressHUDHelperb__;
    }

    public int getCFProgressHUDHelperc__() {
        return CFProgressHUDHelperc__;
    }

    public void setCFProgressHUDHelperc__(int CFProgressHUDHelperc__) {
        this.CFProgressHUDHelperc__ = CFProgressHUDHelperc__;
    }

    public int getCFProgressHUDHelperd__() {
        return CFProgressHUDHelperd__;
    }

    public void setCFProgressHUDHelperd__(int CFProgressHUDHelperd__) {
        this.CFProgressHUDHelperd__ = CFProgressHUDHelperd__;
    }

    private void CFProgressHUDHelper1(){
        CFProgressHUDHelpera__ = 0;
        CFProgressHUDHelperb__ = 1;
        if(CFProgressHUDHelperb__  == 0){
            CFProgressHUDHelpera__ = (CFProgressHUDHelpera__+CFProgressHUDHelperb__);
            CFProgressHUDHelperd__ = (CFProgressHUDHelperc__+CFProgressHUDHelperb__);
        }
    }

    private void CFProgressHUDHelper2(){
        CFProgressHUDHelpera__ = CFProgressHUDHelpera__*3;
        CFProgressHUDHelperb__ = CFProgressHUDHelperb__*3;
        if(CFProgressHUDHelpera__ == 1){
            CFProgressHUDHelperc__ = (CFProgressHUDHelpera__+CFProgressHUDHelperb__);
            CFProgressHUDHelperd__ = (CFProgressHUDHelpera__+CFProgressHUDHelperb__);
        }
    }

    private void CFProgressHUDHelper3(){
        CFProgressHUDHelperc__ = 0;
        CFProgressHUDHelperd__ = CFProgressHUDHelpera__*3;
        CFProgressHUDHelpera__ = CFProgressHUDHelperc__*3;
        if(CFProgressHUDHelperd__ == 1){
            CFProgressHUDHelperc__ = (CFProgressHUDHelpera__+CFProgressHUDHelperb__);
            CFProgressHUDHelperd__ = (CFProgressHUDHelpera__+CFProgressHUDHelperb__);
        }
    }

    private void CFProgressHUDHelper4(){
        CFProgressHUDHelperd__ = 0;
        CFProgressHUDHelperc__ = CFProgressHUDHelperb__*3;
        CFProgressHUDHelpera__ = CFProgressHUDHelperc__*3;
        if(CFProgressHUDHelperd__ == 1) {
            CFProgressHUDHelperc__ = (CFProgressHUDHelpera__ + CFProgressHUDHelperb__);
            CFProgressHUDHelperd__ = (CFProgressHUDHelpera__ + CFProgressHUDHelperb__);
        }
    }
}